import 'package:flutter/material.dart';
import 'package:collins_vocabulary/components/book_options.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:collins_vocabulary/components/plan_option.dart';

class Mine extends StatefulWidget {
  final SharedPreferences prefs;
  Mine({Key key, this.prefs}) : super(key: key);

  @override
  MineState createState() => MineState();
}

class MineState extends State<Mine> {
  bool showColins;
  bool autoSound;
  bool sentence;
  bool showCn;
  bool enPh;
  int count;
  @override
  void initState() {
    super.initState();
    showColins = widget.prefs.getBool('showcollins');
    sentence = widget.prefs.getBool('sentence');
    showCn = widget.prefs.getBool('showCn');
    autoSound = widget.prefs.getBool('autoplay');
    count = widget.prefs.getInt('count');
    enPh = widget.prefs.getBool('en_ph');
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Container(
        width: MediaQuery.of(context).size.width,
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [
              Color(0xFF845ec2),
              Color(0xFFd65db1),
              Color(0xFFff9671),
              Color(0xFFffc75f),
            ],
            end: Alignment.topLeft,
            begin: Alignment.bottomCenter,
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SizedBox(height: 100),
            InkWell(
              onTap: () {
                Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => SelectBook(prefs: widget.prefs),
                  ),
                );
              },
              child: Container(
                width: 160,
                padding: EdgeInsets.all(16),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text(
                      '切换单词书',
                      style: TextStyle(fontSize: 14, color: Colors.white),
                    ),
                    Icon(Icons.chevron_right, size: 17, color: Colors.white)
                  ],
                ),
              ),
            ),
            InkWell(
              onTap: () async {
                await Navigator.of(context).push(
                  MaterialPageRoute(
                    builder: (context) => ChangePlan(prefs: widget.prefs),
                  ),
                );
                setState(() {
                  count = widget.prefs.getInt('count');
                });
              },
              child: Container(
                width: 160,
                padding: EdgeInsets.all(16),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          '当前复习计划',
                          style: TextStyle(fontSize: 14, color: Colors.white),
                        ),
                        Text(
                          '每天$count个单词',
                          style: TextStyle(
                            fontSize: 10,
                            color: Colors.white.withOpacity(.8),
                          ),
                        ),
                      ],
                    ),
                    Icon(Icons.chevron_right, size: 17, color: Colors.white)
                  ],
                ),
              ),
            ),
            Container(
              width: 220,
              child: Stack(
                children: [
                  Positioned(
                    top: 40,
                    left: 17,
                    child: Text(
                      '不开启默认使用美式发音',
                      style: TextStyle(
                        fontSize: 10,
                        color: Colors.white.withOpacity(.8),
                      ),
                    ),
                  ),
                  SwitchListTile(
                    activeColor: Colors.white,
                    title: Text(
                      '使用英式发音',
                      style: TextStyle(fontSize: 14, color: Colors.white),
                    ),
                    value: enPh,
                    onChanged: (bool value) {
                      setState(
                        () {
                          enPh = value;
                          widget.prefs.setBool('en_ph', value);
                        },
                      );
                    },
                  ),
                ],
              ),
            ),
            SizedBox(height: 20),
            Container(
              width: 220,
              child: SwitchListTile(
                activeColor: Colors.white,
                title: Text(
                  '开启柯林斯词典',
                  style: TextStyle(fontSize: 14, color: Colors.white),
                ),
                value: showColins,
                onChanged: (bool value) {
                  setState(() {
                    showColins = value;
                    widget.prefs.setBool('showcollins', value);
                  });
                },
              ),
            ),
            Container(
              width: 220,
              child: SwitchListTile(
                activeColor: Colors.white,
                title: Text(
                  '读音自动播放',
                  style: TextStyle(fontSize: 14, color: Colors.white),
                ),
                value: autoSound,
                onChanged: (bool value) {
                  setState(
                    () {
                      autoSound = value;
                      widget.prefs.setBool('autoplay', value);
                    },
                  );
                },
              ),
            ),
            Container(
              width: 220,
              child: SwitchListTile(
                activeColor: Colors.white,
                title: Text(
                  '显示中文翻译',
                  style: TextStyle(fontSize: 14, color: Colors.white),
                ),
                value: showCn,
                onChanged: (bool value) {
                  setState(
                    () {
                      showCn = value;
                      widget.prefs.setBool('showCn', value);
                    },
                  );
                },
              ),
            ),
            Container(
              width: 220,
              child: SwitchListTile(
                activeColor: Colors.white,
                title: Text(
                  '显示例句',
                  style: TextStyle(fontSize: 14, color: Colors.white),
                ),
                value: sentence,
                onChanged: (bool value) {
                  setState(
                    () {
                      sentence = value;
                      widget.prefs.setBool('sentence', value);
                    },
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
